$(document).ready(function(){
  $.fn.imagedd = function(o){
    this.each(function(i){
      var obj = $(this);
      var img = obj.find("img");
      var dsrc = img.attr("dsrc");
      var islok = true;
      if (dsrc.length != 0) {
        //添加事件
        var a = img.attr("alt");
        var imgdiv = $('<div class="imgdiv" style="width:'+o[4]+'px;height:'+o[5]+';"><img /></div>');
          obj.prepend(imgdiv);
          imgdiv.hide();
        obj.hover(function() {
          if(islok){
            imgdiv.find("img").attr("src",dsrc);
            islok = 0;
          }
          imgdiv.show();
          var ml;
          if(o[0] == 0) ml = o[1];
          else ml = (i%o[0]) * o[1];
          imgdiv.stop().animate({
            marginTop: '-'+o[6]+'px',
            marginLeft: '-'+ml+'px',
            width: o[2]+'px',
            height: o[3]+'px',
            padding: '0px'

          },
          200);
          if (a.length != 0) {
            obj.prepend('<span class="imgtitle" style="margin-left: -'+ml+'px;margin-top:-'+o[7]+'px;">'+ a + '</span>');
            $('.imgtitle').animate({
              marginLeft: '-'+ml+'px',
              marginTop: o[7]+'px'
            },
            400).css({
              'z-index': '10',
              'position': 'absolute',
              'float': 'left'
            })
          }
        },
        function() {
          imgdiv.stop().animate({
            marginTop: '0',
            marginLeft: '0',
            width: o[4]+'px',
            height: o[5]+'px',
            padding: '0px'
          },
          400,null,function(){
            imgdiv.hide();
          });
          obj.find('.imgtitle').remove();
        });
      }

    });
  };
  $('#imgl li').imagedd([4,220,800,533,140,100,380,145]);
  $('#img04').imagedd([4,220,800,533,290,250,250,275]);
  $('#oomap').imagedd([0,180,1230,800,160,180,600,180]);

	$('#demoxx2').bxCarousel({
		display_num: 5, 
		move: 1, 
		auto: true,
		margin: 10,
		auto_hover: true
	});

	 

});

var MDChdtvFocus=function(){
  this.id=jQuery(arguments[0].id);
  this.bigImgW=arguments[0].bigImgW;
  this.bigImgH=arguments[0].bigImgH;
  this.smallImgW=arguments[0].smallImgW;
  this.smallImgH=arguments[0].smallImgH;
  this.speed=arguments[0].speed||5000;
  this.focusData=arguments;
  this.autoPlay;
  this.autoPlay2;
};
MDChdtvFocus.prototype={
  init:function(){
    var that=this;
    this.id.addClass("MDChdtvFocus");   
    var go=function(){          
      that.id.css("backgroundImage","none");          
      var _bigImg=jQuery('<div class="l" style="width:'+that.bigImgW+'px;height:'+that.bigImgH+'px"><a target="_blank"><img style="display:none;width:100%;height:100%" /></a><span></span><div class="bg"></div><div style="width:'+that.bigImgW+'px"><h1></h1><p></p></div></div>');
      var _tab=jQuery('<div class="r"><span class="up"><a href="#"></a></span><span class="down"><a href="#"></a></span><div><dl></dl></div></div>');     
      that.id.html(_bigImg).append(_tab);
      for(var i=1;i<that.focusData.length;i++){       
        that.id.find("> div:eq(1) dl").append('<dd><img src="'+that.focusData[i][1]+'" width="'+that.smallImgW+'" height="'+that.smallImgH+'" /></dd>');
      };      
      that.a()
    };
    go();
  },
  a:function(){
    var _tab=this.id.find("> div:eq(1)"),that=this;
    var _num=this.id.find(".r dl").height()-360;
    var gogo=function(i){
      that.b(i);
    };
    gogo();
    this.id.find(".r a").click(function(){
      return false;
    });
    this.id.find(".r > .up").mousedown(function(){
      if(that.id.find(".r > div dl").position().top<0){
        if(!that.id.find(".r > div dl").is(":animated")){
          that.id.find(".r > div dl").stop().animate({top:"+=90"},200);
        }
      }
    });
    this.id.find(".r > .down").mousedown(function(){
      if(that.id.find(".r > div dl").position().top>-_num){
        if(!that.id.find(".r > div dl").is(":animated")){
          that.id.find(".r > div dl").stop().animate({top:"-=90"},200);
        }
      } 
    });
    this.id.find(".l").hover(function(){
      that.id.find(".l span").addClass("now");
    },function(){
      that.id.find(".l span").removeClass("now");
    }).click(function(){
      window.open(that.id.find(".l a:eq(0)").attr("href"));
      return false;
    });
    _tab.find("dd").each(function(i){
      (function(i){
        _tab.find("dd").eq(i).bind("mouseenter",function(){
          if(jQuery(this)[0].className!="now"){
            clearTimeout(that.autoPlay);
            clearTimeout(that.autoPlay2); 
            gogo(i+1);
            clearTimeout(that.autoPlay);
            clearTimeout(that.autoPlay2);
          }else{
            clearTimeout(that.autoPlay);
            clearTimeout(that.autoPlay2);
          }
        }).bind("mouseleave",function(){
          clearTimeout(that.autoPlay);          
          that.autoPlay2=setTimeout(function(){
            var s=i+2;
            s>that.focusData.length-1?s=1:s;
            gogo(s);
          },that.speed);
        }).click(function(){
          window.open(that.focusData[i+1][2])
        });
      })(i)
    })
  },
  b:function(i){
    var _index=i||1,that=this;
    this.id.find("img:eq(0)").attr("src",that.focusData[_index][0]).parent().attr("href",that.focusData[_index][2]).end().css({"display":"block"});
    this.id.find("dd").removeClass("now").eq(_index-1).addClass("now");
    this.id.find(".l > div > h1").html(that.focusData[_index][3]);
    this.id.find(".l > div > p").html(that.focusData[_index][4]);
    var _w=this.id.find(".l > div:eq(1)").width();
    this.id.find(".l > div:eq(0)").css("width",0).stop().animate({width:_w},200);
    var _num1=this.id.find(".r dl dd.now").prevAll().length*90;
    var _num2=this.id.find(".r dl").height()-360;
    var _num3=_num1-270;
    if(!that.id.find(".r > div dl").is(":animated")){
      if(that.id.find(".r > div dl").position().top > -(_num1-270)){
        that.id.find(".r > div dl").stop().animate({top:-_num3},200);
      };
      if(that.id.find(".r > div dl").position().top < (-_num1)){        
        that.id.find(".r > div dl").stop().animate({top:-_num1},200);
      }
    }
    if(that.id.find(".r > div dd").eq(0)[0].className=="now"){      
      if(!that.id.find(".r > div dl").is(":animated")){       
        that.id.find(".r > div dl").stop().animate({top:0},200);
      }
    };
    this.autoPlay=setTimeout(function(){
      that.b(_index==that.focusData.length-1?_index=1:++_index);
    },that.speed);
  }
}